package zf.data.formats
{
	public class DateFormat
	{
		/**
		 * 转化date为日期字符串 
		 * @param date 
		 * @param separator 分隔符,默认为 - ,返回例如 1999-01-01
		 * @return 
		 * 
		 */
		public static function toDateString(date:Date=null,separator:String="-"):String
		{
			if(date==null)
			{
				date=new Date();
			}
			var year:Number=date.fullYear;
			var month:Number=date.month+1;
			var da:Number=date.date;
			
			return year+separator+toDoubleDigit(month)+separator+toDoubleDigit(da);
		}
		/**
		 * 转化date为日期时间字符串 
		 * @param date
		 * @param separator 分隔符,默认为 - ,返回例如 1999-01-01-12-05-06
		 * @return 
		 * 
		 */
		public static function toDateTimeString(date:Date=null,separator:String="-"):String
		{
			var s0:String=toDateString(date,separator);
			var s1:String=toTimeString(date,separator);
			return s0+separator+s1;
		}
		public static function DateStringToDate(dateString:String,separator:String="-"):Date
		{
			var arr:Array=dateString.split(separator);
			if(arr.length<3)
			{
				throw new Error("dateString is error.");
				return null;
			}
			var newArr:Array=new Array();
			var i:uint;
			try
			{
				var total:uint=arr.length>6?6:arr.length;
				for(i=0;i<total;i++)
				{
					newArr.push(uint(arr[i]));
				}
			}catch(e:Error)
			{
				throw new Error("dateString is error.");
				return null;
			}
			i=0;
			var date:Date=new Date(newArr[i++],newArr[i++]-1,newArr[i++]);
			var leng:uint=newArr.length;
			if(leng>5)
			{
				date.setHours(newArr[i++],newArr[i++],newArr[i++]);
			}else if(leng>4)
			{
				date.setHours(newArr[i++],newArr[i++]);
			}else if(leng>3)
			{
				date.setHours(newArr[i++]);
			}
			return date;
		}
		/**
		 * 转化date为中文日期字符串 
		 * @param date 
		 * @param includeWeek 是否包含星期
		 * @return 例如 2000年1月10日 星期五
		 * 
		 */
		public static function toCnDateString(date:Date=null,includeWeek:Boolean=false):String
		{
			if(date==null)
			{
				date=new Date();
			}
			var year:Number=date.fullYear;
			var month:Number=date.month+1;
			var da:Number=date.date;
			
			var str:String=year+"年"+toDoubleDigit(month)+"月"+toDoubleDigit(da)+"日";
			
			if(includeWeek)
			{
				str+=" "+getCnWeek(date);
			}
			return str;
		}
		/**
		 * 转化date为时间字符串 
		 * @param date
		 * @param separator 分隔符,默认为 - ,返回例如 19:08:15
		 * @return 
		 * 
		 */
		public static function toTimeString(date:Date=null,separator:String=":"):String
		{
			if(date==null)
			{
				date=new Date();
			}
			var hour:Number=date.hours;
			var minutes:Number=date.minutes;
			var seconds:Number=date.seconds;
			
			return hour+separator+toDoubleDigit(minutes)+separator+toDoubleDigit(seconds);
		}
		
		/**
		 * 转化date为短时间字符串 ,包含小时和分钟
		 * @param date
		 * @param separator 分隔符,默认为 - ,返回例如 19:08
		 * @return 
		 * 
		 */
		public static function toShortTimeString(date:Date=null,separator:String=":"):String
		{
			if(date==null)
			{
				date=new Date();
			}
			var hour:Number=date.hours;
			var minutes:Number=date.minutes;
			
			return hour+separator+toDoubleDigit(minutes);
		}
		
		/**
		 * 获得中文星期几 
		 * @param date
		 * @return 例：星期三
		 * 
		 */
		public static function getCnWeek(date:Date=null):String
		{
			if(date==null)
			{
				date=new Date();
			}
			var dayStr:String="";
			var list:Array=new Array("日","一","二","三","四","五","六")
			dayStr="星期"+list[date.day];
			return dayStr;
		}
		
		/**
		 * 转化为中文date字符串 
		 * @param date
		 * @param includeWeek 是否包含星期几
		 * @return 例：2011年3月15日 星期三 19:09:23
		 * 
		 */
		public static function toCnString(date:Date=null,includeWeek:Boolean=false):String
		{
			if(date==null)
			{
				date=new Date();
			}
			return toCnDateString(date,includeWeek)+" "+toTimeString(date);
		}
		
		/**
		 * 转化为date字符串 
		 * @param date
		 * @return 例：2002-12-12 15:24:08
		 * 
		 */
		public static function toString(date:Date=null):String
		{
			if(date==null)
			{
				date=new Date();
			}
			return toDateString(date)+" "+toTimeString(date);
		}
		
		/**
		 * 转化为2位数字表现，例如 9 将转化为 09 
		 * @param num
		 * @return 
		 * 
		 */
		private static function toDoubleDigit(num:uint):String
		{
			if(num<10)
			{
				return "0"+num;
			}else if(num<100)
			{
				return num+"";
			}else
			{
				return num%100+"";
			}
		}
	}
}